<?php
/***
* @version $Id: admin_modules.php 336 2007-01-23 08:12:56Z flexiondotorg $
* @copyright (c) 2006 - 2007 Flexion.Org
*            (c) 2004 - 2006 CyberAlien (http://www.phpbbstyles.com)
*            (c) 2004 - 2005 Project Minerva
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

define('IN_R3BORN', true);

if (!empty($setmodules))
{
	$module['Modules']['Module_version'] = basename(__FILE__);
	return;
}

$root_path = './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require('./pagestart.' . $phpEx);

$updates = array();

//Translate the data in $mvModule to what the
foreach ($mvModules as $name => $value)
{
	$updates[$name]['update_name'] = $value['displayname'];
	$updates[$name]['update_type'] = $value['type'];
	$updates[$name]['update_url']  = $value['version_check'];
	$updates[$name]['update_version'] = $value['version'];
	$updates[$name]['update_item'] = $name;
}

ksort($updates);

// nothing to update
if(!count($updates))
{
	message_die(GENERAL_ERROR, $lang['Module_update_nothing']);
}

// show list of available updates
if(!isset($_GET['doupdate']))
{
	$template->set_filenames(array('body' => 'module_version_list.tpl'));
	$template->assign_vars(array(
		'UPDATE_URL'		=> append_sid('admin_modules_version.'.$phpEx.'?doupdate=1'),
		'L_UPDATE_TOTAL1'	=> str_replace('{NUM}', count($updates), $lang['Module_update_total1']),
		)
	);
	$counter = 0;
	@reset($updates);
	foreach($updates as $var => $item)
	{
		$counter ++;
		$type = isset($lang['Module_update_types'][$item['update_type']]) ? $item['update_type'] : 0;

		$row_class = ( !($counter % 2) ) ? 'row1' : 'row2';
		$template->assign_block_vars('row',
				array(
				'ROW_CLASS'	=> $row_class,
				'NUM'		=> $counter,
				'VAR'		=> 'item_'.$counter.'_',
				'ITEM'		=> htmlspecialchars($var),
				'NAME'		=> htmlspecialchars($item['update_name']),
				'TYPE'		=> $lang['Module_update_types'][$type],
				'URL'		=> htmlspecialchars($item['update_url']),
				'VERSION'	=> htmlspecialchars($item['update_version'])
				)
			);
		$template->assign_block_vars('row.'.(empty($item['update_url']) ? 'nourl' : 'url'), array());
	}
	$template->pparse('body');
	page_footer_admin();
}

// check updates.

// getting list of items to update
@reset($updates);
$urls = array();
$items = array();
$i=0;
foreach($updates as $var1 => $item)
{
	$i++;
	$var = 'item_'.$i.'_';
	if(!empty($_POST[$var.'item']) && !empty($_POST[$var.'checked']) && $_POST[$var.'checked'])
	{
		$item = $_POST[$var.'item'];
		if(!empty($updates[$item]['update_url']))
		{
			$items[] = $var1;
			$found = false;
			$url = $updates[$item]['update_url'];
			for($j=0; $j<count($urls) && !$found; $j++)
			{
				if($urls[$j] === $url)
				{
					$found = true;
				}
			}
			if(!$found)
			{
				$urls[] = $url;
			}
		}
	}
	if(isset($updates[$var1]['data']))
	{
		unset($updates[$var1]['data']);
	}
}

// showing error message if there is nothing to update
if(!count($urls))
{
	message_die(GENERAL_ERROR, $lang['Module_update_nothing']);
}

@set_time_limit(intval($_POST['timeout']));

// getting data
for($i=0; $i<count($urls); $i++)
{
	$arr = @file($urls[$i]);
	if(empty($arr))
	{
		// cannot connect. show it as error message
		@reset($items);
		for($j=0; $j<count($items); $j++)
		{
			$item = $updates[$items[$j]];
			if($item['update_url'] === $urls[$i])
			{
				$updates[$items[$j]]['data']['error'] = $lang['Module_update_error_noconnect'];
			}
		}
	}
	else
	{
		for($j=0; $j<count($arr); $j++)
		{	// trim all lines and replace tab with space
			$arr[$j] = trim(str_replace("\t", ' ', $arr[$j]));
		}
		// checking all items to see which ones are for this url
		for($j=0; $j<count($items); $j++)
		{
			$item = $updates[$items[$j]];
			if($item['update_url'] === $urls[$i])
			{
				// searching for data for this item
				$begin_text = '<!-- BEGIN ' . $item['update_item'] . ' -->';
				$end_text = '<!-- END ' . $item['update_item'] . ' -->';
				$begin_pos = -1;
				$end_pos = -1;
				// getting begin and end tags for it
				for($k=0; ($k<count($arr)-1) && ($begin_pos < 0); $k++)
				{
					if($arr[$k] === $begin_text)
					{
						$begin_pos = $k;
						for(; ($k<count($arr)) && ($end_pos < 0); $k++)
						{
							if($arr[$k] === $end_text)
							{
								$end_pos = $k;
							}
						}
						if($end_pos < 0)
						{
							$end_pos = count($arr);
						}
					}
				}
				$data = array();
				// found item position in text
				if($begin_pos >= 0)
				{
					// getting all data for this item in array
					for($k=$begin_pos+1; $k<$end_pos; $k++)
					{
						$arr2 = explode(' ', $arr[$k], 2);
						if(count($arr2) == 2)
						{
							$data[trim($arr2[0])] = trim($arr2[1]);
						}
					}
				}
				else
				{
					$data['error'] = $lang['Module_update_error_noitem'];
				}
				$updates[$items[$j]]['data'] = $data;
			}
		}
	}
}

$template->set_filenames(array('body' => 'module_version_result.tpl'));

@reset($updates);
$count_total = 0;
$count_error = 0;
$count_update = 0;
foreach($updates as $var => $item)
{
	if(isset($item['data']) && is_array($item['data']))
	{
		$count_total++;
		$type = isset($lang['Module_update_types'][$item['update_type']]) ? $item['update_type'] : 0;
		$ver1 = htmlspecialchars($item['update_version']);

		$row_class = ( !($count_total % 2) ) ? 'row1' : 'row2';
		$template->assign_block_vars('row',
			array(
				'ROW_CLASS'		=> $row_class,
				'ITEM'			=> htmlspecialchars($item['update_name']),
				'TYPE'			=> $lang['Module_update_types'][$type],
				'VERSION'		=> $ver1,
			)
		);
		if(!empty($item['data']['version']))
		{
			$ver2 = htmlspecialchars($item['data']['version']);
			$info = isset($item['data']['info']) ? $item['data']['info'] : '';

			$vCompare = compare_versions($ver1, $ver2);

			if ($vCompare == 0)
			{
				//Versions are the same
				$template->assign_block_vars('row.noupdate',
					array(
						'VERSION'		=> $ver2,
						'MESSAGE'		=> $lang['Module_update_noupdate'],
						'INFO'			=> empty($info) ? '' : htmlspecialchars($info),
					)
				);
				$template->assign_block_vars('row.noupdate.' . (empty($item['data']['info']) ? 'noinfo' : 'info'), array());
			}
			else if ( $vCompare < 0 && (!empty($item['data']['update']) || !empty($item['data']['autoupdate'])) )
			{
				// Available version is newer than the current version
				$count_update++;

				$template->assign_block_vars('row.update',
					array(
						'NUM'			=> $count_total,
						'VERSION'		=> $ver2,
						'UPDATE'		=> isset($item['data']['update']) ? htmlspecialchars($item['data']['update']) : '',
						'INFO'			=> htmlspecialchars($info),
					)
				);
				$template->assign_block_vars('row.update.' . (empty($item['data']['update']) ? 'noupdate' : 'updated'), array());
				$template->assign_block_vars('row.update.' . (empty($item['data']['info']) ? 'noinfo' : 'info'), array());
			}
			else if ( $vCompare > 0 )
			{
				// Current version is newer than available version
				$template->assign_block_vars('row.newer',
					array(
						'VERSION'		=> $ver2,
						'MESSAGE'		=> $lang['Module_update_newer'],
						'INFO'			=> empty($info) ? '' : htmlspecialchars($info),
					)
				);
				$template->assign_block_vars('row.newer.' . (empty($item['data']['info']) ? 'noinfo' : 'info'), array());
			}
			else
			{
				$template->assign_block_vars('row.noupdate',
					array(
						'VERSION'		=> $ver2,
						'MESSAGE'		=> $lang['Module_update_noupdate'],
						'INFO'			=> empty($info) ? '' : htmlspecialchars($info),
					)
				);
				$template->assign_block_vars('row.noupdate.' . (empty($item['data']['info']) ? 'noinfo' : 'info'), array());
			}
		}
		else
		{
			if(empty($item['data']['error']))
			{
				$item['data']['error'] = $lang['Module_update_error_noitem'];
			}
			$template->assign_block_vars('row.error', array('ERROR' => htmlspecialchars($item['data']['error'])));
			$count_error++;
		}
	}
}

$template->assign_vars(
	array(
		'COUNT_TOTAL'		=> str_replace('{NUM}', $count_total, $lang['Module_update_total1']),
		'COUNT_ERROR'		=> str_replace('{NUM}', $count_error, $lang['Module_update_total2']),
		'COUNT_UPDATE'		=> str_replace('{NUM}', $count_update, $lang['Module_update_total3'])
	)
);

$template->pparse('body');
page_footer_admin();

?>